#!/bin/bash

function clean_object_files()
{
    local makeBin="$1"
    local homeworkRoot="$2"
    
    # run the clean command
    $makeBin -C $homeworkRoot clean > /dev/null
}

function run_compile()
{
    local timeBin="$1"
    local makeBin="$2"
    local homeworkRoot="$3"
    local compileFlag="$4"
    local compileOptionName="OPT_FLAGS"
    local parallelOption="$5"
    
    # running the compile command
    $timeBin $makeBin -C $homeworkRoot -j "$parallelOption" OPT_FLAGS="$compileFlag" 2>&1
}

# script to gather data for part 3.3 of the homework

TIME_BIN="/usr/bin/time"
MAKE_BIN="/usr/bin/make"
HW1_DIR="/homes/a/abbasmus/courses/454ece/hw1"

COMPILE_OPT_FLAGS="-O3"

pushd $HW1_DIR

echo "numberOfParallelCompiles" "AVERAGE TIME" "SPEED UP"

for numberOfParallelCompiles in 1 2 4 8; do
    echo -n "$numberOfParallelCompiles" ""

    #run the compile 5 times
    for ((compileCount=0; compileCount<5; compileCount++)); do
        clean_object_files "$MAKE_BIN" "$HW1_DIR"
        run_compile "$TIME_BIN" "$MAKE_BIN" "$HW1_DIR" "$COMPILE_OPT_FLAGS" "$numberOfParallelCompiles" | grep elapsed | awk '{print $3}' | sed 's,elapsed,,g' | sed 's,0:,,g'
    done |
    awk '{sum += $1} END {print sum/5}'
done

clean_object_files "$MAKE_BIN" "$HW1_DIR"

popd
